/*
 * eventhandler.cpp
 * This file is part of OpenAnimator
 *
 * Copyright (C) 2008 - Abhinav Lele
 *
 * OpenAnimator is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenAnimator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenAnimator; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */
 
 
#include "eventhandler.h"


EventKeeper::~EventKeeper() {
	map< unsigned long, map<string, string, ltstr_stl>* >::iterator tempiter;
	for(tempiter = repository.begin();tempiter != repository.end(); ++tempiter)
		delete tempiter->second;	
}

extern EventKeeper myEventKeeper;
/**Check for events and call the appropriate handlers */

void EventChecker::do_events() {
	SDL_Event event_data;
	int flag;
	unsigned long event_fingerprint = 0;
	map< unsigned long, map<string,string,ltstr_stl>* >::iterator fingerprint_iter;
	map<string,string,ltstr_stl>::iterator assoc_iter;
	do {
		flag = SDL_PollEvent(&event_data);
		if(!flag) continue;
		switch(event_data.type) {
			case SDL_KEYDOWN:
				event_fingerprint |=  ( TYPE_KEYBOARD | KEYPRESSED ); break;	
			case SDL_KEYUP:
				event_fingerprint |=  ( TYPE_KEYBOARD | KEYRELEASED ); break;
			case SDL_MOUSEMOTION:
				event_fingerprint |=  ( TYPE_MOUSEMOVE ); break;
			case SDL_MOUSEBUTTONDOWN:
				event_fingerprint |=  ( TYPE_MOUSEBUTTON | MOUSEPRESSED ); break;
			case SDL_MOUSEBUTTONUP:
				event_fingerprint |=  ( TYPE_MOUSEBUTTON ); break;	
		};	
	        	
	}while(flag);
	if( event_fingerprint == 0) return; /* Nothing to process */
	for(fingerprint_iter = myEventKeeper.repository.begin(); fingerprint_iter != myEventKeeper.repository.end(); fingerprint_iter++) {
		if(fingerprint_iter->first & event_fingerprint == event_fingerprint) {
			for(assoc_iter = fingerprint_iter->second->begin(); assoc_iter != fingerprint_iter->second->end(); assoc_iter++) {
				myEventKeeper.script_land.call(assoc_iter->second,assoc_iter->first); /* call name , arg(obj name) */
			}
		}
	}
}
